using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class DrawMesh : MonoBehaviour
{
    private MeshFilter meshFilter;
    private Mesh mesh;
    private MeshRenderer meshRenderer;
    //定义顶点数组
    private Vector3[] vertices = new Vector3[100];
    //存放三角面的顺序，所以一定是3的倍数
    private int[] triangels = new int[486];
 
    //给绘制的正方形附材质，材质贴图是一个四边形图片，
    private Vector2[] uvs;
    //正方形的材质球
    public Material mat;
    void Start()
    {
        meshFilter = GetComponent<MeshFilter>();
        mesh = meshFilter.mesh;
        meshRenderer = GetComponent<MeshRenderer>();
        //  DrawTriangle();
        // DrawSquare();
        // DrawSquareWithTex();
        DrawTriangle();
    }
    /// <summary>
    /// 绘制一个三角面
    /// </summary>
    void DrawTriangle()
    {
        //定义上图的ABC三个顶点
        vertices = new Vector3[] { Vector3.zero, Vector3.forward, new Vector3(1, 0, 1) };
        //定义三个顶点的顺序，使其正面朝上 0表示第一个顶点，1表示第二个顶点
        triangels = new int[] { 0, 1, 2 };
        //在构造Mesh前最好clear下，如果Mesh再后来复用的话，一定要清空否则索引容易出现差错
        mesh.Clear();
        //给mesh赋值
        mesh.vertices = vertices;
        mesh.triangles = triangels;
    }
 
    /// <summary>
    /// 绘制一个正方形(这是单面的)
    /// </summary>
    void DrawSquare()
    {
        //定义上图的ABCD四个顶点
        vertices = new Vector3[] { Vector3.zero, Vector3.forward, new Vector3(1, 0, 1) ,Vector3.right};
        //0,1,2表示顺序ABC；2，3，0表示顺序CDA 两个三角面
        triangels = new int[] { 0, 1, 2,2,3,0 };
        //在构造Mesh前最好clear下，如果Mesh再后来复用的话，一定要清空否则索引容易出现差错
        mesh.Clear();
        //给mesh赋值
        mesh.vertices = vertices;
        mesh.triangles = triangels;
    }
    /// <summary>
    ///绘制一个正方形并给生成的mesh赋予贴图
    /// </summary>
    void DrawSquareWithTex()
    {
        //定义上图的ABCD四个顶点
        vertices = new Vector3[] { Vector3.zero, Vector3.forward, new Vector3(1, 0, 1), Vector3.right };
        //0,1,2表示顺序ABC；2，3，0表示顺序CDA 两个三角面
        triangels = new int[] { 0, 1, 2, 2, 3, 0 };
        //图片UV的四个点位置       
        uvs = new Vector2[] { Vector2.zero, new Vector2(0, 1), Vector2.one, new Vector2(1, 0) };
        //在构造Mesh前最好clear下，如果Mesh再后来复用的话，一定要清空否则索引容易出现差错
        mesh.Clear();
        //给mesh赋值
        mesh.vertices = vertices;
        mesh.triangles = triangels;
        //给正方形赋予材质球贴图
        mesh.uv = uvs;
        meshRenderer.material = mat;
 
    }
    /// <summary>
    /// 绘制双面的正方形
    /// </summary>
    void DrawDoubleSquareWithTex()
    {
        //定义正方形正反面的顶点
        vertices = new Vector3[] 
        { Vector3.zero, Vector3.forward, new Vector3(1, 0, 1), Vector3.right,//正面点
          Vector3.zero, Vector3.forward, new Vector3(1, 0, 1), Vector3.right//背面点
        };
        //0,1,2表示顺序ABC；2，3，0表示顺序CDA 两个三角面, 4,7,6   6,5,4 表示反的三角面
        triangels = new int[] { 0, 1, 2,   2, 3, 0 ,    4,7,6,  6,5,4       };
        //  uvs = new Vector2[] { Vector2.zero, new Vector2(0, 1), Vector2.one, new Vector2(1, 0) };
        //重新分配UV值，正面u是0到0.5的
        uvs = new Vector2[]
        { Vector2.zero, new Vector2(0, 1), new Vector2(0.5f, 1), new Vector2(0.5f, 0),//正面
         new Vector2(0.5f, 0), new Vector2(0.5f, 1),Vector2.one, new Vector2(1, 0)//反面
        };
        //在构造Mesh前最好clear下，如果Mesh再后来复用的话，一定要清空否则索引容易出现差错
        mesh.Clear();
        //给mesh赋值
        mesh.vertices = vertices;
        mesh.triangles = triangels;
        //给正方形赋予材质球贴图
        mesh.uv = uvs;
        meshRenderer.material = mat;
    }
}